package com.dm.library.utils;

import android.content.Context;
import android.text.TextUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * 版    权:  深圳市迪蒙网络科技有限公司
 * 描    述:  <描述>
 * Created by tangjian on 2016/9/6.
 */
public class ResourceUtils {

    public ResourceUtils() {
    }

    /***
     * 根据字符串名称获取对应的ID
     *
     * @param context
     * @param resType
     * @param resName
     * @return
     */
    public static int getResId(Context context, String resType, String resName) {
        int resId = 0;
        if (context != null && !TextUtils.isEmpty(resType) && !TextUtils.isEmpty(resName)) {

            String packageName = context.getPackageName();
            if (TextUtils.isEmpty(packageName)) {
                return resId;
            }

            resId = context.getResources().getIdentifier(resName, resType, packageName);
            if (resId <= 0) {
                resId = context.getResources().getIdentifier(resName.toLowerCase(), resType, packageName);
            }
        }
        if (resId <= 0) {
            System.err.println("failed to parse " + resType + " resource \"" + resName + "\"");
        }
        return resId;
    }

//    /****
//     * 获取drawable文件夹下的ID
//     *
//     * @param mContext
//     * @param resName
//     * @return
//     */
//    public static int getBitmapRes(Context mContext, String resName) {
//        return getResId(mContext, "drawable", resName);
//    }

    /****
     * 获取drawable文件夹下的ID
     *
     * @param context
     * @param resName
     * @return
     */
    public static int getBitmapRes(Context context, String resName) {
        return getResId(context, "mipmap-hdpi", resName);
    }

    /****
     * 获取string文件夹下的ID
     *
     * @param context
     * @param resName
     * @return
     */
    public static int getStringRes(Context context, String resName) {
        return getResId(context, "string", resName);
    }

    /****
     * 获取array文件夹下的ID
     *
     * @param context
     * @param resName
     * @return
     */
    public static int getStringArrayRes(Context context, String resName) {
        return getResId(context, "array", resName);
    }

    /****
     * 获取layout文件夹下的ID
     *
     * @param context
     * @param resName
     * @return
     */
    public static int getLayoutRes(Context context, String resName) {
        return getResId(context, "layout", resName);
    }

    /****
     * 获取style文件夹下的ID
     *
     * @param context
     * @param resName
     * @return
     */
    public static int getStyleRes(Context context, String resName) {
        return getResId(context, "style", resName);
    }

    /****
     * 获取id文件夹下的ID
     *
     * @param context
     * @param resName
     * @return
     */
    public static int getIdRes(Context context, String resName) {
        return getResId(context, "id", resName);
    }

    /****
     * 获取color文件夹下的ID
     *
     * @param context
     * @param resName
     * @return
     */
    public static int getColorRes(Context context, String resName) {
        return getResId(context, "color", resName);
    }

    /****
     * 获取raw文件夹下的ID
     *
     * @param context
     * @param resName
     * @return
     */
    public static int getRawRes(Context context, String resName) {
        return getResId(context, "raw", resName);
    }

    /****
     * plurals
     *
     * @param context
     * @param resName
     * @return
     */
    public static int getPluralsRes(Context context, String resName) {
        return getResId(context, "plurals", resName);
    }

    /****
     * 获取anim文件夹下的ID
     *
     * @param context
     * @param resName
     * @return
     */
    public static int getAnimRes(Context context, String resName) {
        return getResId(context, "anim", resName);
    }

    public static void deleteFilesInFolder(File folder) throws Throwable {
        if (folder != null && folder.exists()) {
            if (folder.isFile()) {
                folder.delete();
            } else {
                String[] names = folder.list();
                if (names != null && names.length > 0) {
                    String[] arr$ = names;
                    int len$ = names.length;

                    for (int i$ = 0; i$ < len$; ++i$) {
                        String name = arr$[i$];
                        File f = new File(folder, name);
                        if (f.isDirectory()) {
                            deleteFilesInFolder(f);
                        } else {
                            f.delete();
                        }
                    }

                }
            }
        }
    }

    public static void deleteFileAndFolder(File folder) throws Throwable {
        if (folder != null && folder.exists()) {
            if (folder.isFile()) {
                folder.delete();
            } else {
                String[] names = folder.list();
                if (names != null && names.length > 0) {
                    String[] arr$ = names;
                    int len$ = names.length;

                    for (int i$ = 0; i$ < len$; ++i$) {
                        String name = arr$[i$];
                        File f = new File(folder, name);
                        if (f.isDirectory()) {
                            deleteFileAndFolder(f);
                        } else {
                            f.delete();
                        }
                    }

                    folder.delete();
                } else {
                    folder.delete();
                }
            }
        }
    }

    public static long getFileSize(String path) throws Throwable {
        if (TextUtils.isEmpty(path)) {
            return 0L;
        } else {
            File file = new File(path);
            return getFileSize(file);
        }
    }

    public static long getFileSize(File file) throws Throwable {
        if (!file.exists()) {
            return 0L;
        } else if (!file.isDirectory()) {
            return file.length();
        } else {
            String[] names = file.list();
            int size = 0;

            for (int i = 0; i < names.length; ++i) {
                File f = new File(file, names[i]);
                size = (int) ((long) size + getFileSize(f));
            }

            return (long) size;
        }
    }

    public static boolean saveObjectToFile(String filePath, Object object) {
        if (!TextUtils.isEmpty(filePath)) {
            File cacheFile = null;

            try {
                cacheFile = new File(filePath);
                if (cacheFile.exists()) {
                    cacheFile.delete();
                }

                if (!cacheFile.getParentFile().exists()) {
                    cacheFile.getParentFile().mkdirs();
                }

                cacheFile.createNewFile();
            } catch (Throwable var6) {
                var6.printStackTrace();
                cacheFile = null;
            }

            if (cacheFile != null) {
                try {
                    FileOutputStream t = new FileOutputStream(cacheFile);
                    GZIPOutputStream gzos = new GZIPOutputStream(t);
                    ObjectOutputStream oos = new ObjectOutputStream(gzos);
                    oos.writeObject(object);
                    oos.flush();
                    oos.close();
                    return true;
                } catch (Throwable var7) {
                    var7.printStackTrace();
                }
            }
        }

        return false;
    }

    public static Object readObjectFromFile(String filePath) {
        if (!TextUtils.isEmpty(filePath)) {
            File cacheFile = null;

            try {
                cacheFile = new File(filePath);
                if (!cacheFile.exists()) {
                    cacheFile = null;
                }
            } catch (Throwable var6) {
                var6.printStackTrace();
                cacheFile = null;
            }

            if (cacheFile != null) {
                try {
                    FileInputStream t = new FileInputStream(cacheFile);
                    GZIPInputStream gzis = new GZIPInputStream(t);
                    ObjectInputStream ois = new ObjectInputStream(gzis);
                    Object object = ois.readObject();
                    ois.close();
                    return object;
                } catch (Throwable var7) {
                    var7.printStackTrace();
                }
            }
        }

        return null;
    }
}
